﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using DTO;

namespace DAO
{
    public class DAOKhoi:DataProvider
    {
        public static DataTable getAll()
        {            
            string select = "select * from khoi";
            OleDbDataAdapter da = new OleDbDataAdapter(select,Connect());
            DataSet ds = new DataSet();
            da.Fill(ds, "khoi");
            Disconnect();
            return ds.Tables[0];
        }

        public static bool isDuplicate(DTOKhoi khoi)
        {
            ketnoi = Connect();
            string select = "select * from khoi where tenkhoi=@tenkhoi";
            OleDbDataAdapter da = new OleDbDataAdapter(select, ketnoi);
            da.SelectCommand.Parameters.AddWithValue("@tenkhoi", khoi.TenKhoi);
            new OleDbCommandBuilder(da);
            DataSet ds = new DataSet();
            da.Fill(ds);

            Disconnect();
            return ds.Tables[0].Rows.Count > 0 ? true : false;
        }

        public static void Add(string tenkhoi)
        {
            ketnoi = Connect();            
            string select = "select * from khoi";
            OleDbDataAdapter da = new OleDbDataAdapter(select, ketnoi);
            new OleDbCommandBuilder(da);
            DataSet ds = new DataSet();
            da.FillSchema(ds, SchemaType.Source, "khoi");            

            DataRow dr = ds.Tables[0].NewRow();
            dr["tenkhoi"] = tenkhoi;
            ds.Tables[0].Rows.Add(dr);

            da.Update(ds,"khoi");
            Disconnect();
        }

        public static void Edit(int makhoi,string tenkhoi)
        {
            ketnoi = Connect();
            string select = "select * from khoi";
            OleDbDataAdapter da = new OleDbDataAdapter(select, ketnoi);
            new OleDbCommandBuilder(da);
            DataSet ds = new DataSet();
            da.FillSchema(ds, SchemaType.Source, "khoi");
            da.Fill(ds, "khoi");

            DataRow dr = ds.Tables[0].Rows.Find(makhoi);
            dr.BeginEdit();
            dr["tenkhoi"] = tenkhoi;
            dr.EndEdit();           

            da.Update(ds, "khoi");
            Disconnect();           
        }

        public static void Delete(int makhoi)
        {
            ketnoi = Connect();
            string select = "select * from khoi";
            OleDbDataAdapter da = new OleDbDataAdapter(select, ketnoi);
            new OleDbCommandBuilder(da);
            DataSet ds = new DataSet();
            da.FillSchema(ds, SchemaType.Source, "khoi");
            da.Fill(ds, "khoi");

            DataRow dr = ds.Tables[0].Rows.Find(makhoi);
            dr.Delete();

            da.Update(ds, "khoi");
            Disconnect();            
        }
    }
}
